import 'dart:convert' show json;
import 'package:flutter/cupertino.dart';
import 'base_chart_axis.dart';
import 'base_chart_formatter.dart';
import 'base_model_asT.dart';
import 'package:ribo_base_core/base_styles/base_chart_style.dart';

class BaseChartTwoSidesBarModel {
  /// https://echarts.apache.org/zh/option.html#tooltip.formatter
  BaseChartFormatter tooltipFormatter;

  /// https://echarts.apache.org/zh/option.html#series-pie.label.formatter
  BaseChartFormatter labelFormatter;

  /// X坐标轴formatter
  BaseChartFormatter xAxisLabelFormatter;

  /// y坐标轴formatter
  BaseChartFormatter yAxisLabelFormatter;

  /// 坐标轴显示设置
  BaseChartAxis axis;

  /// 图表样式
  BaseChartStyle style;

  /// 图表数据
  List<BaseChartTwoSidesBarData> data;

  BaseChartTwoSidesBarModel({
    this.tooltipFormatter,
    this.labelFormatter,
    this.xAxisLabelFormatter,
    this.yAxisLabelFormatter,
    this.style,
    this.axis,
    @required this.data,
  });

  factory BaseChartTwoSidesBarModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<BaseChartTwoSidesBarData> data =
        jsonRes['data'] is List ? <BaseChartTwoSidesBarData>[] : null;
    if (data != null) {
      for (final dynamic item in jsonRes['data']) {
        if (item != null) {
          data.add(BaseChartTwoSidesBarData.fromJson(
              asT<Map<String, dynamic>>(item)));
        }
      }
    }

    return BaseChartTwoSidesBarModel(
      tooltipFormatter: asT<BaseChartFormatter>(jsonRes['tooltipFormatter']),
      labelFormatter: asT<BaseChartFormatter>(jsonRes['labelFormatter']),
      xAxisLabelFormatter:
          asT<BaseChartFormatter>(jsonRes['xAxisLabelFormatter']),
      yAxisLabelFormatter:
          asT<BaseChartFormatter>(jsonRes['yAxisLabelFormatter']),
      axis: asT<BaseChartAxis>(jsonRes['axis']),
      style: asT<BaseChartStyle>(jsonRes['style']),
      data: data,
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'tooltipFormatter': tooltipFormatter,
        'labelFormatter': labelFormatter,
        'xAxisLabelFormatter': xAxisLabelFormatter,
        'yAxisLabelFormatter': yAxisLabelFormatter,
        'axis': axis,
        'style': style,
        'data': data,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartTwoSidesBarData {
  BaseChartTwoSidesBarData({String name, this.data, this.itemStyle}):this.name=name.replaceFirst('-', '-\n').replaceAll(' ', '\n');

  factory BaseChartTwoSidesBarData.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartTwoSidesBarData(
              name: asT<String>(jsonRes['name']),
              data: asT<List<BaseChartTwoSidesBarDataLegendData>>(
                  jsonRes['data']),
              itemStyle:
                  asT<BaseChartTwoSidesBarDataItemStyle>(jsonRes['itemStyle']),
            );

  /// 数据名称
  String name;

  /// 数据值
  List<BaseChartTwoSidesBarDataLegendData> data;

  BaseChartTwoSidesBarDataItemStyle itemStyle;

  Map<String, dynamic> toJson() =>
      <String, dynamic>{'name': name, 'data': data, 'itemStyle': itemStyle};

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartTwoSidesBarDataLegendData {
  BaseChartTwoSidesBarDataLegendData({
    String name,
    this.value,
  }):this.name=name.replaceFirst('-', '-\n').replaceAll(' ', '\n');

  factory BaseChartTwoSidesBarDataLegendData.fromJson(
          Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartTwoSidesBarDataLegendData(
              name: asT<String>(jsonRes['name']),
              value: asT<double>(jsonRes['value']),
            );

  /// 数据名称
  String name;

  /// 数据值
  double value;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'name': name,
        'value': value,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartTwoSidesBarDataItemStyle {
  String color;
  String borderColor;

  Map<String, dynamic> toJson() {
    return {'color': color, 'borderColor': borderColor};
  }

  BaseChartTwoSidesBarDataItemStyle({this.color, this.borderColor});
}
